import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    routes: [{
            path: '',
            redirect: '/login',
        },
        {
            path: '/login',
            name: 'login',
            component: () =>
                import ('@/components/login/login')
        },
        {
            path: '/userHome',
            name: 'userHome',
            component: () =>
                import ('@/views/userHome')
        },
        {
            path: '/userMain',
            name: 'userMain',
            redirect: '/userMain/personData',
            component: () =>
                import ('@/views/userMain/userMain'),
            children: [{
                    path: '/userMain/personData',
                    name: 'personData',
                    component: () =>
                        import ('@/views/userMain/userMainHome/personData/personData'),
                },
                {
                    path: '/userMain/mesBox',
                    name: 'mesBox',
                    component: () =>
                        import ('@/views/userMain/userMainHome/mesBox/mesBox'),
                },
                {
                    path: '/userMain/artCommit',
                    name: 'artCommit',
                    component: () =>
                        import ('@/views/userMain/userMainHome/artCommit/artCommit'),
                },
                {
                    path: '/userMain/adminHome',
                    name: 'adminHome',
                    component: () =>
                        import ('@/views/userMain/userMainHome/adminReq/adminHome'),
                }
            ],
        },
    ]
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        // location.reload();
        return next();
    }
    const isNext = window.sessionStorage.getItem('token')
    if (!isNext) {
        // location.reload();
        return next('/login')
    }
    next();
})
export default router